<template>
  <template v-for="subItem in menuList" :key="subItem.path">
    <el-sub-menu v-if="subItem.children?.length" :index="subItem.path">
      <template #title>
        <div @contextmenu.prevent="openRightMenu($event, subItem.path)" class="menu-title-wrapper">
          <svg-icon v-if="subItem.meta.icon" :icon="subItem.meta.icon" class="el-icon" />
          <span class="sle">{{ subItem.meta.title }}</span>
        </div>
      </template>
      <SubMenu :menu-list="subItem.children" />
    </el-sub-menu>
    <el-menu-item v-else :index="subItem.path" @click="handleClickMenu(subItem)" @contextmenu.prevent="openRightMenu($event, subItem.path)">
      <div class="menu-item-wrapper">
        <svg-icon v-if="subItem.meta.icon" :icon="subItem.meta.icon" class="el-icon" />
        <span class="sle">{{ subItem.meta.title }}</span>
      </div>
    </el-menu-item>
  </template>
</template>

<script setup lang="ts">
import { ref } from "vue";
import { useRouter } from "vue-router";
import ContextMenu from "./components/ContextMenu.vue";

defineProps<{ menuList: Menu.MenuOptions[] }>();

const router = useRouter();
const contextMenuRef = ref<InstanceType<typeof ContextMenu>>();
const currentMenuPath = ref<string>("");

const handleClickMenu = (subItem: Menu.MenuOptions) => {
  if (subItem.meta.isLink) return window.open(subItem.meta.isLink, "_blank");
  router.push(subItem.path);
};

// 打开右键菜单
const openRightMenu = (event: MouseEvent, menuPath: string) => {
  currentMenuPath.value = menuPath;
  contextMenuRef.value?.show(event);
};
</script>

<style lang="scss">
.el-sub-menu .el-sub-menu__title:hover {
  color: var(--el-menu-hover-text-color) !important;
  background-color: transparent !important;
}
.el-menu--collapse {
  .is-active {
    .el-sub-menu__title {
      color: #ffffff !important;
      background-color: var(--el-color-primary) !important;
    }
  }
}
.el-menu-item {
  &:hover {
    color: var(--el-menu-hover-text-color);
  }
  &.is-active {
    color: var(--el-menu-active-color) !important;
    background-color: var(--el-menu-active-bg-color) !important;
    &::before {
      position: absolute;
      top: 0;
      bottom: 0;
      width: 4px;
      content: "";
      background-color: var(--el-color-primary);
    }
  }
}
.vertical,
.classic,
.transverse {
  .el-menu-item {
    &.is-active {
      &::before {
        left: 0;
      }
    }
  }
}
.columns {
  .el-menu-item {
    &.is-active {
      &::before {
        right: 0;
      }
    }
  }
}

.menu-title-wrapper,
.menu-item-wrapper {
  display: flex;
  align-items: center;
  width: 100%;

  .el-icon {
    margin-right: 5px;
  }
}
</style>
